var gOption = {
	"appid" : "40", //产品的appid
	"domain" : location.host, //产品的web域名
	"xlbtid" : "3",
	"haboaid":	"xiazaibao_center_wap"//移动端是xiazaibao_center_wap
}

var getParameter = function (name) {
	var pattern = new RegExp("[?&]" + name + "\=([^&#]+)", "g");
	try {
		var matcher = pattern.exec(document.location.href);
	} catch (e) {};
	if (null != matcher) {
		return decodeURIComponent(matcher[1]);
	} else {
		return null;
	}
};
var c_getCookie=function(e){
	var t=e+"=",n=document.cookie.indexOf(t);
	if(n!=-1){
		n+=t.length;
		var r=document.cookie.indexOf(";",n);
		return r==-1&&(r=document.cookie.length),unescape(document.cookie.substring(n,r))
	}
	return""
};

var c_setCookie=function(d,e,t,n){
	if(arguments.length>3){
		var i=new Date((new Date).getTime()+n*36e5);
		document.cookie=e+"="+escape(t)+";path=/;domain="+d+";expires="+i.toGMTString()
	}
	else
		document.cookie=e+"="+escape(t)+";path=/;domain="+d
};

//获取cookieid
var habo_web_uid=c_getCookie("HABOWEBUID");
if(!habo_web_uid||habo_web_uid=="undefined"){
	var random=Math.random();
	var browser=navigator.appName+"_"+navigator.appVersion+"_"+navigator.userAgent+"_"+navigator.appCodeName+"_"+navigator.platform;
	var nowtime=new Date;
	var nowtime_sec=nowtime.valueOf();
	habo_web_uid=hex_md5(nowtime_sec.toString()+browser+random.toString());
	c_setCookie(gOption.domain,"HABOWEBUID",habo_web_uid,87600)
}


//获取sessionid
var habo_web_sessionid=c_getCookie("HABOWEBSESSIONID");
if(!habo_web_sessionid||habo_web_sessionid=="undefined"){
	var random=Math.random();
	var browser=navigator.appName+"_"+navigator.appVersion+"_"+navigator.userAgent+"_"+navigator.appCodeName+"_"+navigator.platform;
	var nowtime=new Date;
	var nowtime_sec=nowtime.valueOf();
	habo_web_sessionid=hex_md5(nowtime_sec.toString()+browser+random.toString());
	c_setCookie(gOption.domain,"HABOWEBSESSIONID",habo_web_sessionid,0.5);
}

//获取来源页
var ref=document.referrer;
var referfrom = getParameter('referfrom');
if(!referfrom || referfrom=="undefined"){
    referfrom = c_getCookie('referfrom_xzb');
    if(!referfrom || referfrom=="undefined"){
        referfrom = 'xzb';
    }else{
        c_setCookie(gOption.domain,"referfrom_xzb",referfrom,87600)
    }
}else{
    c_setCookie(gOption.domain,"referfrom_xzb",referfrom,87600)
}


//获取xl账号ID,当用xl账号登陆成功时，需要c_setCookie(gOption.domain,"userid",userid)

var userid=c_getCookie("userid")&&c_getCookie("HABOWEBSESSIONID")?parseInt(c_getCookie("userid")):0;
//获取useragent信息
var userAgent = navigator.userAgent;
var userAgent_e = encodeURIComponent(userAgent);

//获取url
var url=document.location.href;


var ref_e=encodeURIComponent(ref);
var url_e=encodeURIComponent(url);


function objevent(objname,objevent,objfun){
	var objname=String(objname);
	if(objevent=="")
		objevent="onclick";
	else
		var objevent=String(objevent);
	var objfun=String(objfun),thisevent=function(evt){
		evt=evt||window.event;var obj=evt.target||evt.srcElement,objnametemp1=String(obj.tagName),objnametemp2=String(objname);
		if(objnametemp1==objnametemp2||objname==""||obj.parentNode.tagName==objnametemp2||obj.parentNode.parentNode.tagName==objnametemp2){
			if(objnametemp1!=objnametemp2)
				if(obj.parentNode.tagName==objnametemp2)
					obj=obj.parentNode;
				else if(obj.parentNode.parentNode.tagName==objnametemp2)
					obj=obj.parentNode.parentNode;
			else{
				if(obj.parentNode.parentNode.parentNode.tagName!=objnametemp2)
					return;
				obj=obj.parentNode.parentNode.parentNode
			}
			eval(objfun)(obj)
		}
	},thiseventtemp="document.body."+objevent+"="+thisevent,evalobj=eval(thiseventtemp);evalobj
}
//js绑定onclick事件
function _clickon(e){
	send_web_click(e);
}

try{
	var _kk_click_pv_clickon_handler=function(){
	objevent("A","onclick","_clickon");
	};
	function kk_click_pv_rebind_capture(){
		window.attachEvent?(window.detachEvent("onload",_kk_click_pv_clickon_handler),window.attachEvent("onload",_kk_click_pv_clickon_handler)):(window.removeEventListener("load",_kk_click_pv_clickon_handler,!0),window.addEventListener("load",_kk_click_pv_clickon_handler,!0))
	}
	kk_click_pv_rebind_capture()
}
catch(e){}


//页面展示的发包
function send_web_pv(){
	//alert("call send_web_pv()");
	if (!Date.now) {
		Date.now = function() { return new Date().getTime(); }
	}
	var unix_timestamp = Math.floor(Date.now() / 1000);
	var furl="http://stat.download.xunlei.com:8099/web_pv?url="+url_e+"&ref="+ref_e+"&useragent="+userAgent_e+"&userid="+userid+"&cookieid="+habo_web_uid+"&sessionid="+habo_web_sessionid+"&appid="+gOption.appid+"&r="+Math.random()*1e5+"&xlbtid="+gOption.xlbtid+"&actid=xzbgw_2016&haboaid="+gOption.haboaid+'&time='+unix_timestamp;;
	var clickid = document.getElementById('webPV').getAttribute('data-clickid');
	var eventid = document.getElementById('webPV').getAttribute('data-eventid');



	if(clickid && eventid){
		furl = furl+'&eventid='+encodeURIComponent(eventid)+'&clickid='+encodeURIComponent(clickid);
		furl = furl+'&referfrom='+encodeURIComponent(referfrom);
		var r=new Image;
		r.src=furl;
	}


	//document.write('<img src="'+furl+'" width="0" height="0" frameborder="0" />');
	//有别的发包字段，则直接在furl上追加，furl=furl+"key1="+value1,value1需要uriencode
	//document.write('<img src="'+furl+'" width="0" height="0" frameborder="0" />');
	//alert(furl);
	//send_request(furl);
}

//点击事件的发包
// function jumpurl(clickurl){
// 	location.href = clickurl;
// }
function send_web_click(e){
	//alert("call send_web_click()")
	var clickurl = e.getAttribute("_click_rcv_url");
	if(!clickurl || clickurl == "undefined"){
		clickurl = e.href;
	}
	var clickurl_e = encodeURIComponent(clickurl);
	var eventid = e.getAttribute('data-eventid');
	var clickid = e.getAttribute('data-clickid');
	if (!Date.now) {
		Date.now = function() { return new Date().getTime(); }
	}
	var unix_timestamp = Math.floor(Date.now() / 1000);
	if(eventid && clickid){
		var furl="http://stat.download.xunlei.com:8099/web_click?url="+url_e+"&useragent="+userAgent_e+"&userid="+userid+"&cookieid="+habo_web_uid+"&sessionid="+habo_web_sessionid+"&appid="+gOption.appid+"&clickurl="+clickurl_e+"&eventid="+eventid+"&clickid="+clickid+"&r="+Math.random()*1e5+"&xlbtid="+gOption.xlbtid+"&actid=xzbgw_2016&haboaid="+gOption.haboaid+'&time='+unix_timestamp;
        furl = furl+'&referfrom='+encodeURIComponent(referfrom);

		var r=new Image();
		r.src=furl;
	}

	//有别的发包字段，则直接在furl上追加，furl=furl+"key1="+value1,value1需要uriencode -->


	//document.write('<img src="'+furl+'" width="0" height="0" frameborder="0" />');
	// if(target !== "_blank"){
	// 	setTimeout("jumpurl('"+clickurl+"');",100);
	// }
	//alert(furl);
	//alert('111');
	//send_request(furl);
	//document.getElementById('tstmsg').innerHTML="<span>" + furl + "</span>";
	//alert(furl);
}

function sendEvent(eventid, clickid){
	//alert("call send_web_click()")
	if (!Date.now) {
		Date.now = function() { return new Date().getTime(); }
	}
	var unix_timestamp = Math.floor(Date.now() / 1000);
	if(eventid && clickid){
		var furl="http://stat.download.xunlei.com:8099/web_click?url="+url_e+"&useragent="+userAgent_e+"&userid="+userid+"&cookieid="+habo_web_uid+"&sessionid="+habo_web_sessionid+"&appid="+gOption.appid+"&clickurl=&eventid="+eventid+"&clickid="+clickid+"&r="+Math.random()*1e5+"&xlbtid="+gOption.xlbtid+"&actid=xzbgw_2016&haboaid="+gOption.haboaid+'&time='+unix_timestamp;
        furl = furl+'&referfrom='+encodeURIComponent(referfrom);

		var r=new Image();
		r.src=furl;
	}

	//有别的发包字段，则直接在furl上追加，furl=furl+"key1="+value1,value1需要uriencode -->


	//document.write('<img src="'+furl+'" width="0" height="0" frameborder="0" />');
	// if(target !== "_blank"){
	// 	setTimeout("jumpurl('"+clickurl+"');",100);
	// }
	//alert(furl);
	//alert('111');
	//send_request(furl);
	//document.getElementById('tstmsg').innerHTML="<span>" + furl + "</span>";
	//alert(furl);
}

window.send_web_pv = send_web_pv


export const pv = {
	init:function(type){
		if(type === 'activity'){
			send_web_pv()
		}
	},
	sendEvent:sendEvent
}
